package com.lzz.bottomencapsulation.net;

import android.content.Context;
import android.text.TextUtils;

import com.lzz.bottomencapsulation.consts.NetConsts;
import com.lzz.bottomencapsulation.utils.LUtils;
import com.lzz.bottomencapsulation.utils.NetUtils;
import com.lzz.bottomencapsulation.utils.TUtils;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;


public class SubscriberTwo<D, R extends RequestCallBackTwo<D>> implements Observer<HttpRequest<D>> {
    String TAG="网络请求";
    R r;
    Context context;
    public SubscriberTwo(R r, Context context) {
        this.r = r;
        this.context = context;
    }

    @Override
    public void onError(Throwable e) {
        String code=NetConsts.NETWORK_REQUEST_METHOD_ERROR;
        String msg=NetConsts.NETWORK_REQUEST_METHOD_ERROR_STR;
        if (!NetUtils.isConnected(context)) {
            code=NetConsts.NETWORK_NOT_CONNECTED;
            msg=NetConsts.NETWORK_NOT_CONNECTED_STR;
            TUtils.showShort(context,msg);
        }
        r.onStop();
        r.onRequestfail(code, msg);
        LUtils.v(TAG,"网络请求错误======"+e.toString());
    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        r.onStart();
    }

    @Override
    public void onNext(HttpRequest<D> tHttpRequestData) {
        r.onStop();
        if (tHttpRequestData == null || TextUtils.isEmpty(tHttpRequestData.getErrno())) {
            LUtils.v(TAG,"没有返回值");
            r.onRequestfail(NetConsts.NETWORK_REQUEST_METHOD_REQUESTDATANUll, NetConsts.NETWORK_REQUEST_METHOD_REQUESTDATANUll_STR);
        }else if (tHttpRequestData.getErrno().equals(NetConsts.ERROR_CODE_OK) ) {
            r.onRequestSuccess(tHttpRequestData.getData(),"成功");
        } else if (tHttpRequestData.getErrno().equals("200")){
//            元宝兑换金币需要审核
            r.onRequestfail(tHttpRequestData.getErrno(),tHttpRequestData.getData());
        }else  {
            r.onRequestfail(tHttpRequestData.getErrno(),tHttpRequestData.getErrmsg());
        }
    }
}

